﻿using KouDai.WinPad.Common;
using KouDai.WinPad.Helper;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

// The Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234233

namespace KouDai.WinPad.Views
{
    public sealed partial class ShellPage : LayoutAwarePage
    {
        private double virtualizingStackPanelHorizontalOffset;

        public ShellPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            OutGridView.ItemsSource = CollectionSource.View.CollectionGroups;
        }

        protected override void LoadState(object navigationParameter, Dictionary<string, object> pageState)
        {
            base.LoadState(navigationParameter, pageState);

            //重新导航到起始位置
            if (pageState != null && pageState.ContainsKey("virtualizingStackPanelHorizontalOffset"))
            {
                virtualizingStackPanelHorizontalOffset = double.Parse(pageState["virtualizingStackPanelHorizontalOffset"].ToString(), CultureInfo.InvariantCulture.NumberFormat);
            }
        }

        protected override void SaveState(System.Collections.Generic.Dictionary<string, object> pageState)
        {
            base.SaveState(pageState);
            //保存导航记录
            var virtualizingStackPanel = TreeHelper.GetVisualChild<VirtualizingStackPanel>(itemGridView);
            if (virtualizingStackPanel != null && pageState != null)
            {
                pageState["virtualizingStackPanelHorizontalOffset"] = virtualizingStackPanel.HorizontalOffset;
            }
        }

        private void VirtualizingStackPanel_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var virtualizingStackPanel = (VirtualizingStackPanel)sender;
            virtualizingStackPanel.SetHorizontalOffset(virtualizingStackPanelHorizontalOffset);
        }

    }
}
